﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import Classes.*;
    import Classes.util.*;
    import com.adobe.utils.*;

    public class UpgradeActions {

        public static const MECHANIC_NAME:String = "upgradeHelpers";

        protected var m_resource:MapResource;
        protected var m_cachedFriendHelpers:Array = null;
        protected var m_cachedFriendHelperActions:int = 0;

        public function UpgradeActions(_arg1:MapResource){
            this.m_resource = _arg1;
        }
        protected function lazyInitializeFriendHelpers():void{
            if (this.m_cachedFriendHelpers != null){
                return;
            };
            this.m_resource.addEventListener(GenericObjectEvent.MECHANIC_DATA_CHANGED, this.onMechanicDataChanged, false, 0, true);
            this.refreshHelperData();
        }
        private function refreshHelperData():void{
            var _local6:String;
            this.m_cachedFriendHelpers = [];
            var _local1:IMechanicUser = (this.m_resource as IMechanicUser);
            if (_local1 == null){
                return;
            };
            var _local2:IDictionaryDataMechanic = (MechanicManager.getInstance().getMechanicInstance(_local1, MECHANIC_NAME, MechanicManager.ALL) as IDictionaryDataMechanic);
            if (_local2 == null){
                return;
            };
            var _local3:UpgradeHelperDefinition = this.getFriendHelperDefinition();
            if (_local3 == null){
                return;
            };
            var _local4:Array = _local2.getKeys();
            var _local5:int;
            for each (_local6 in _local4) {
                this.m_cachedFriendHelpers.push(GameUtil.formatServerUid(_local6));
                _local5++;
                if (_local5 >= _local3.max){
                    break;
                };
            };
            this.m_cachedFriendHelperActions = (this.m_cachedFriendHelpers.length * _local3.actionValue);
        }
        protected function onMechanicDataChanged(_arg1:GenericObjectEvent):void{
            if (_arg1.obj == MECHANIC_NAME){
                this.refreshHelperData();
            };
        }
        public function getTotal():int{
            return ((this.getResourceActions() + this.getHelperActions()));
        }
        public function getResourceActions():int{
            return (this.m_resource.upgradeActionCount);
        }
        public function getHelperActions():int{
            this.lazyInitializeFriendHelpers();
            return (this.m_cachedFriendHelperActions);
        }
        public function getFriendHelperIds():Array{
            this.lazyInitializeFriendHelpers();
            return (ArrayUtil.copyArray(this.m_cachedFriendHelpers));
        }
        public function getFriendHelperDefinition():UpgradeHelperDefinition{
            var _local2:UpgradeHelperDefinition;
            var _local1:Item = this.m_resource.getItem();
            if (((_local1.upgrade) && (_local1.upgrade.helpers))){
                for each (_local2 in _local1.upgrade.helpers) {
                    if (_local2.type == UpgradeHelperDefinition.TYPE_FRIEND){
                        return (_local2);
                    };
                };
            };
            return (null);
        }

    }
}//package Classes.sim 
